// packageD/auction/auction-mine/auctionIntegralCash/auctionIntegralCash.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cash: '',
    radio: '1',
    checkList: [],
    pay_type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    try {
      var value = wx.getStorageSync('mailLanguage');
      let basic_info = wx.getStorageSync("yz_basic_info");
      if (value) {
        let mailLanguage = JSON.parse(value);
        this.setData({
          name_of_withdrawal: mailLanguage.income && mailLanguage.income.name_of_withdrawal ? mailLanguage.income.name_of_withdrawal : '提现',
          balance_name: wx.getStorageSync('balance')
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  SelectHandle(e) {
    this.setData({
      pay_type: e.detail,
      show1: false
    })
  },
  async allSelectHandle(e) {
    this.setData({
      checkList: e.detail,
      countTotal:await this.countTotalPrice(e.detail)
    })
  },
  // 计算总金额
  countTotalPrice(detail) {
    let arr = [];
    detail.forEach((element, ind) => {
      arr[ind] = this.data.datasource[element]
    });
    let total = 0;
    arr.forEach(element => {
      total += Number(element.money)
    });
    return total.toFixed(2);
  },
  getWithdrawType() {
    this.setData({
      show1: true
    })
  },
  show1Tap() {
    this.setData({
      show1: false
    })
  },
  userValinp(e) {
    console.log(e.detail);
    let ind = e.detail;
    this.setData({
      radio: ind
    });
  },
  iscash(e) {
    this.setData({
      cash: e.detail.value
    });
  },
  sureCash() {
    let that = this;
    let idsarr = [];
    this.data.checkList.forEach((element, index) => {
      idsarr.push(this.data.datasource[element].id);
    });
    if (this.data.checkList.length == 0) {
      app.tips('请选择需要提现的保证金')
      return
    }
    if (!this.data.pay_type) {
      app.tips('请选择提现方式')
      return
    }
    let json = {
      ids: idsarr,
      withdraw_type: this.data.pay_type == 'wechat' ? 1 : this.data.pay_type == 2 ? 2 : 7
    };
    let urlStr = app.getNetAddresss('plugin.auction.api.prepayment.withdraw.withdraw');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none',
            success() {
              that.data.checkList.forEach(element => {
                that.data.checkList.splice(element, 1)
              });
              that.setData({
                countTotal: that.countTotalPrice(that.data.checkList)
              })
              that.getData()
            }
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.prepayment.withdraw.can-withdraw');
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            Data: res.data,
            datasource: res.data.list,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
});